﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace OrganaiserProgram
{
    public partial class EditPersonInfo : Form
    {

        private Guid EventId;
        private bool Isnew;

        public EditPersonInfo()
        {
            InitializeComponent();
            notifyIcon1.Icon = new Icon("MiniClock.ico");
            notifyIcon1.Visible = false;
            Isnew = true;
        }

        public void AddSomeText(Guid Id)
        {
            InitializeComponent();
            Isnew = false;

            this.EventId = Id;
        }

        public void AddSimpleMusic(Guid Id)
        {
            InitializeComponent();
            Isnew = false;

            this.EventId = Id;
            
            Stream myStream = null;
            OpenFileDialog saveFileDialog1 = new OpenFileDialog();

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if ((myStream = saveFileDialog1.OpenFile()) != null)
                {
                    // Код по сохранению...
                    string fileName = saveFileDialog1.FileName;
                    foreach (var selectedEvent in DB.InfoList)
                    {
                        if (Id.Equals(selectedEvent.Id))
                        {
                            selectedEvent.EventSound = fileName;
                            return;
                        }
                    }

                    MessageBox.Show(fileName);
                }
            }
        }

        public EditPersonInfo(Guid Id)
        {
            InitializeComponent();
            Isnew = false;

            this.EventId = Id;
            foreach (var selectedEvent in DB.InfoList)
            {
                if (Id.Equals(selectedEvent.Id))
                {
                    var listViewItem = new ListViewItem(selectedEvent.EventName);

                    textBox_Name.Text = selectedEvent.EventName;
                    textBox_EventState.Text = selectedEvent.EventDescription;
                    checkBox_ActiveOrNot.Checked = selectedEvent.Active;
                    comboBox1.Text = selectedEvent.EventsFrequency;
                    dateTimePicker1.Text = selectedEvent.TargetDate;
                    dateTimePicker2.Text = selectedEvent.EventTime;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (Isnew)
            {
                EqualaiserSet es = new EqualaiserSet();

                es.EventName = textBox_Name.Text;
                es.EventDescription = textBox_EventState.Text;
                es.Active = checkBox_ActiveOrNot.Checked;
                if (comboBox1.Text == "")
                {
                    comboBox1.Text = "Day";
                    es.EventsFrequency = comboBox1.Text;
                }
                else es.EventsFrequency = comboBox1.Text;
                es.TargetDate = dateTimePicker1.Text;
                es.EventTime = dateTimePicker2.Text;
                DB.InfoList.Add(es);
            }
            else
            {
                foreach (var selectedEvent in DB.InfoList)
                {
                    if (EventId.Equals(selectedEvent.Id))
                    {
                        var listViewItem = new ListViewItem(selectedEvent.EventName);

                        selectedEvent.EventName = textBox_Name.Text;
                        selectedEvent.EventDescription = textBox_EventState.Text;
                        selectedEvent.Active = checkBox_ActiveOrNot.Checked;
                        selectedEvent.EventsFrequency = comboBox1.Text;
                        selectedEvent.TargetDate = dateTimePicker1.Text;
                        selectedEvent.EventTime = dateTimePicker2.Text;
                    }
                }
            }

            var form1 = (Form1)this.Owner;
            form1.ReloadList();
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void EditPersonInfo_Resize(object sender, EventArgs e)
        {
            if (WindowState == FormWindowState.Minimized)
            {
                Hide();
                notifyIcon1.Visible = true;
            }
        }

        private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                Show();
                WindowState = FormWindowState.Normal;
                notifyIcon1.Visible = false;
            }
        }
    }
}
